﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateAndEvent
{
   
    //事件的发布者
    class EventBuy
    {
        //定义委托
        public delegate void BuyEventHander();
        //定义事件对象
        public event BuyEventHander OnBuy;

        //事件触发器
        public void Issue()
        {
            if (OnBuy !=null)

            {
                Console.WriteLine("水果到了！");
                //触发事件
                OnBuy();


            }
        }
    }
    //时间的订阅者
    class MrZhang
    {
        public void Receive()
        {
            Console.WriteLine("张收到消息，要买苹果");
        }

        //注册事件
        public void Regsiter(EventBuy e) {
            e.OnBuy += Receive;
            e.OnBuy -= Receive;
            //e.OnBuy = Receive;事件不允许直接赋值操作
        }
    }
    class MrWang
    {
        public void Receive()
        {
            Console.WriteLine("王收到消息，要买橙子");
        }

        //注册事件
        public void Regsiter(EventBuy e)
        {
            e.OnBuy += Receive;
        }
    }
}
